﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;
using OOS.Application.File.Services.Interfaces;

namespace OOS.Application.File.Services
{
    public class FileService : IFileService, ITransient
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public String uploadFile(IFormFile file)
        {
            if (file == null) return "文件不能为空";
            var currentDate = DateTime.Now;
            var guid = Guid.NewGuid().ToString("N");
            var filePath = $"/uploads/{currentDate:yyyyMMdd}/{guid}/";
            if (!Directory.Exists("D:/OOS" + filePath))
            {
                Directory.CreateDirectory("D:/OOS" + filePath);
            }
            var fileName = file.FileName;
            // 文件保存
            using (var fs = System.IO.File.OpenWrite("D:/OOS" + filePath + fileName))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //完整的文件路径
            var completeFilePath = Path.Combine(filePath, fileName);
            return "http://192.168.22.1:5003" + completeFilePath;
        }
    }
}
